package tr.gen._3bit.java._2k1s.web.action;

import java.util.List;

import org.apache.struts2.interceptor.validation.SkipValidation;

import tr.gen._3bit.java._2k1s.dao.model.Node;
import tr.gen._3bit.java._2k1s.dao.service.NodeService;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.validator.annotations.IntRangeFieldValidator;
import com.opensymphony.xwork2.validator.annotations.Validation;
import com.opensymphony.xwork2.validator.annotations.VisitorFieldValidator;

@Validation
public class NodeAction extends ActionSupport implements Preparable {
    /**
	 * 
	 */
	private static final long serialVersionUID = 6368758269769595822L;
	private NodeService service;
    private List<Node> nodes;
    private Node node;
    private Integer id;

    public NodeAction(NodeService service) {
        this.service = service;
    }

    @SkipValidation
    public String execute() {
        this.nodes = service.findAll();
        return Action.SUCCESS;
    }

    public String save() {
        this.service.save(node);
        this.node = new Node();
        return execute();
    }
    
    @SkipValidation
    public String remove() {
        service.remove(id);
        return execute();
    }

    public List<Node> getNodes() {
        return nodes;
    }

    @IntRangeFieldValidator(message = "", key = "node.action.error.id.int", min = "0")
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void prepare() throws Exception {
        if (id != null)
            node = service.find(id);
    }

    @VisitorFieldValidator(message="")
    public Node getNode() {
        return node;
    }

    public void setNode(Node node) {
        this.node = node;
    }
}
